package com.htt.app.amnhac12h;

import java.lang.ref.WeakReference;
import java.util.List;

import android.app.Dialog;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnInfoListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Handler;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.htt.app.amnhac12h.adapter.ListShowAdapter;
import com.htt.app.amnhac12h.clock.Clock;
import com.htt.app.amnhac12h.object.Show;
import com.htt.app.amnhac12h.sqlite.DAOShow;
import com.htt.app.amnhac12h.utils.Log;
import com.htt.app.amnhac12h.utils.Utils;
import com.htt.app.amnhac12h.view.PlayerView;

public class PLayerService extends Service implements OnCompletionListener, OnPreparedListener, 
					OnErrorListener,SeekBar.OnSeekBarChangeListener, OnClickListener, OnInfoListener {
	
	public static boolean isRun = false;
	public static boolean isPlaying = false;
	public static boolean isPreparing = false;
	public static boolean isStop = true;
	
	private int channelID = -1;
	
	public static Show listended = null;
	
	private static final String TAG = "PlayerService";
	
	// header
	public  WeakReference<TextView> tvHeaderTitle;
	public  WeakReference<ImageButton> btnHenGio;
	public  WeakReference<ImageButton> btnStar;
	public  WeakReference<TextView> tvHeaderDes;
		
	// image view
	private WeakReference<ImageView> imgView;
	private Thread threadSlide = null;
	
	// timer
	private Clock clock = null;
	public  WeakReference<TextView> tvTimerCurrent;
	public  WeakReference<TextView> tvTimerTotal;
	public  WeakReference<SeekBar> sbTimer;
	
	// button play, (pause), previous, reload
	public  WeakReference<ImageButton> btnPlay, btnPrevious, 
				btnNext, btnBackward, btnForward;
	
	private WeakReference<TextView> tvHenGio;
	
	// Media Player
	public static MediaPlayer mp = null;
	
	// Handler to update UI timer, progress bar etc,.
	private Handler mHandler = new Handler();
	public int currentRadioIndex = 0;
	public static int isRunMedia = -1;
	
	private int seekForwardTime = 30000; // 5000 milliseconds
	private int seekBackwardTime = 30000; // 5000 milliseconds
	
	private PhoneStateListener phoneStateListener;
	private TelephonyManager telephonyManager;
	private boolean isPausedInCall = false;
	
	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		isRun = true;
		mp = new MediaPlayer();
		
		mp.setOnPreparedListener(this);
		mp.setOnErrorListener(this);
		mp.setOnCompletionListener(this);
		mp.setOnInfoListener(this);
		mp.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {

			@Override
			public void onBufferingUpdate(MediaPlayer arg0, int percent) {
				// TODO Auto-generated method stub
				Log.i(TAG, "Buffering: " + percent);
				sbTimer.get().setSecondaryProgress(percent);
			}
			
		});
		super.onCreate();
		
	}
	
	// --------------onStartCommand-------------------------------------------------------------------------//
	@SuppressWarnings("deprecation")
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		isRun = true;
		
		try {
		isRunMedia = intent.getIntExtra("Media", 0);
		initializeUIElements();
		releaseThreadSlide();
		if(isRunMedia == 1) {
			
			if(PlayerView.show != null && 
					( PlayerView.show.getId() != channelID)) {
				
				play(PlayerView.show);
				channelID = PlayerView.show.getId();
				listended = PlayerView.show;

			} else if(PlayerView.show != null && PlayerView.show.getId() == channelID) {
					changeViewRadio(PlayerView.show);
			
			} else if(PlayerView.show == null)
				play(PlayerView.show);
		}
		// Get the telephony manager
				telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
				phoneStateListener = new PhoneStateListener() {
					@Override
					public void onCallStateChanged(int state, String incomingNumber) {
						// String stateString = "N/A";
						Log.v(TAG, "Starting CallStateChange");
						switch (state) {
						case TelephonyManager.CALL_STATE_OFFHOOK:
						case TelephonyManager.CALL_STATE_RINGING://có cuộc g�?i đến
							if (mp != null) {
								pauseMedia();
								isPausedInCall = true;
							}
							break;
						case TelephonyManager.CALL_STATE_IDLE://kết thúc cuộc g�?i
							//Bắt đầu play nhac.
							if (mp != null) {
								if (isPausedInCall) {
									isPausedInCall = false;
									playMedia();
								}
							}
							break;
						}
					}
				};
				// �?ăng ký lắng nge từ việc quản lý điện thoại
				telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
		} catch(Exception e) {
			Utils.showDialog(PlayerView.activity, PlayerView.activity.getResources().getString(R.string.alert), 
					PlayerView.activity.getResources().getString(R.string.error_connect_info));
			e.printStackTrace();
		}
		super.onStart(intent, startId);
		return START_STICKY;
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * Function to play a radio
	 * @param Channel - channel of radio
	 * */
	public void  play(Show c){
			
			if(c != null) 
				PlayerView.show = c;
				
			changeViewRadio(PlayerView.show);
			
			initializePlayer(PlayerView.show);
			
			playRadio();
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		
		case R.id.player_header_home:
			showDialogTimer();
			break;
			
		case R.id.player_btn_forward:
			if(PlayerView.show == null)
				Toast.makeText(PlayerView.activity, PlayerView.activity.getResources().
						getString(R.string.channel_alert_not_select), Toast.LENGTH_SHORT).show();
			else {
				// get current song position
				int currentPosition = mp.getCurrentPosition();
				// check if seekForward time is lesser than song duration
				if (currentPosition + seekForwardTime <= mp.getDuration()) {
					// forward song
					mp.seekTo(currentPosition + seekForwardTime);
				} else {
					// forward to end position
					mp.seekTo(mp.getDuration());
				}
			}
			break;

		case R.id.player_btn_backward:
			if(PlayerView.show == null)
				Toast.makeText(PlayerView.activity, PlayerView.activity.getResources().
						getString(R.string.channel_alert_not_select), Toast.LENGTH_SHORT).show();
			else {
				// get current song position
				int currentPosition2 = mp.getCurrentPosition();
				// check if seekBackward time is greater than 0 sec
				if (currentPosition2 - seekBackwardTime >= 0) {
					// forward song
					mp.seekTo(currentPosition2 - seekBackwardTime);
				} else {
					// backward to starting position
					mp.seekTo(0);
				}
			}
			break;
			
		case R.id.player_header_star:
			if(PlayerView.show == null)
				Toast.makeText(PlayerView.activity, PlayerView.activity.getResources().
						getString(R.string.channel_alert_not_select), Toast.LENGTH_SHORT).show();
			else {

				DAOShow db = new DAOShow(PlayerView.activity);
				db.open();
				if (db.checkShow(PlayerView.show)) {
					db.deleteShow(PlayerView.show);
					PlayerView.show.setStatusFavourite(0);
					btnStar.get().setImageResource(
							R.drawable.icon_star);
				} else {
					PlayerView.show.setStatusFavourite(1);
					db.insertShow(PlayerView.show);
					btnStar.get().setImageResource(R.drawable.icon_star_pressed);
				}
				db.close();

			}
			break;
		case R.id.player_tv_hen_gio:
			showDialogTimer();
			break;
			
		case R.id.player_btn_next:
			if(PlayerView.show == null)
				Toast.makeText(PlayerView.activity, PlayerView.activity.getResources().
						getString(R.string.channel_alert_not_select), Toast.LENGTH_SHORT).show();
			else 
				nextRadio();
			break;
			
		case R.id.player_btn_previous:
			if(PlayerView.show == null)
				Toast.makeText(PlayerView.activity, PlayerView.activity.getResources().
						getString(R.string.channel_alert_not_select), Toast.LENGTH_SHORT).show();
			else 
				previousRadio();
			break;
			
		case R.id.player_btn_play:
			if(PlayerView.show == null)
				Toast.makeText(PlayerView.activity, PlayerView.activity.getResources().
						getString(R.string.channel_alert_not_select), Toast.LENGTH_SHORT).show();
			else 
				playRadio();
			break;
		
		default:
			break;
		}
	}
	
	/**
	 *  show dialog set timer
	 */
	private void showDialogTimer() {
		if(clock == null)
			clock = new Clock(tvHenGio.get(), btnHenGio.get());
			
		final Dialog dialog = new Dialog(PlayerView.activity);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setBackgroundDrawableResource(R.color.boder_dialog);
		dialog.setContentView(R.layout.timer);
		
		final ToggleButton toggleBtn = (ToggleButton) dialog.findViewById(R.id.toggleButton);
		
		final TimePicker timepicker = (TimePicker) dialog.findViewById(R.id.timePicker);
		timepicker.setCurrentHour(Utils.milliSecondsToHours(clock.getTimer()));
		timepicker.setCurrentMinute(Utils.milliSecondsToMinutes(clock.getTimer()));
		timepicker.setIs24HourView(true);
		timepicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
			
			@Override
			public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
				// TODO Auto-generated method stub
				toggleBtn.setChecked(true);
			}
		});

		Button btnXacNhan = (Button) dialog.findViewById(R.id.btnXacNhan);
		Button btnHuy = (Button) dialog.findViewById(R.id.btnHuy);
		
		if(clock.isRuning)
			toggleBtn.setChecked(true);
		else
			toggleBtn.setChecked(false);
		
		btnXacNhan.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			if(toggleBtn.isChecked()) { 
				timepicker.clearFocus();
				long time = (timepicker.getCurrentHour()*60 + timepicker.getCurrentMinute())*60*1000;
				if(clock != null) 
					clock.start(time);
			} else {
				if(clock != null) 
					clock.stop();
			}
		    dialog.dismiss();
			}
		});
		
		btnHuy.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		dialog.show();
	}
	
	/**
	 * Play button click event
	 * plays a radio and changes button to pause image
	 * pauses a radio and changes button to play image
	 * */
	private void playRadio() {

		// check connect internet
		if(Utils.haveNetworkConnection(PlayerView.activity))
			 {
			if(mp != null) {
				if(!isPlaying && !isPreparing && !isStop) {
					
					try {
				    	Log.e(TAG, PlayerView.show.getSource());
				        mp.setDataSource(PlayerView.show.getSource());
				        
				        mp.prepareAsync();
						initNotification();
						// Changing button image to pause button
						btnPlay.get().setImageResource(R.drawable.btn_pause);
						tvTimerTotal.get().setText(R.string.status_connecting);
						
						isPreparing = true;
						isPlaying = false;
				    } catch (Exception e) {
				        Log.e(TAG, "error setting data source");
				        e.printStackTrace();
				        return;
				    }
				    
					
				}
				else {
					if(mp.isPlaying()) {
						mp.pause();
						btnPlay.get().setImageResource(R.drawable.btn_play);
						isPlaying = false;
						isStop = true;
					} else if(isPreparing) {
						btnPlay.get().setImageResource(R.drawable.btn_play);
						tvTimerTotal.get().setText("");
						initializePlayer(PlayerView.show);
					}else {
						mp.start();
						isPlaying = true;
						isStop = false;
						btnPlay.get().setImageResource(R.drawable.btn_pause);
					}
					
				}
			}
		}
		else {
			btnPlay.get().setImageResource(R.drawable.btn_play);
			if(mp.isPlaying()) {
				mp.pause();
				isPlaying = false;
				isStop = true;
			} else if(isPreparing) {
				tvTimerTotal.get().setText("");
				initializePlayer(PlayerView.show);
			}
			Toast.makeText(PlayerView.activity, PlayerView.activity.getResources().getString(R.string.error_connect_info), 
					Toast.LENGTH_SHORT).show();
		}
	}
	
	/**
	 * Next button click event
	 * Plays next radio by taking currentRadioIndex + 1
	 * */
	private void nextRadio() {
		
			if(PlayerView.show == null)
				Toast.makeText(PlayerView.activity, PlayerView.activity.getResources().getString(R.string.channel_not_select), Toast.LENGTH_SHORT).show();
			else {
				
				// check if next radio is there or not
				List<Show> listShows = ListShowAdapter.data;
					if(listShows != null && listShows.size() > 0) {
					currentRadioIndex = Utils.getIndexShow(PlayerView.show, listShows);
					
					if(currentRadioIndex != -1) {
						if(currentRadioIndex < (listShows.size() - 1)){
							play(listShows.get(currentRadioIndex + 1));
							currentRadioIndex = currentRadioIndex + 1;
						}else {
							// play first
							play(listShows.get(0));
							currentRadioIndex = 0;
						}
					}
				}
			}
		}
	
	/**
	 * Back button click event
	 * Plays previous radio by currentRadioIndex - 1
	 * */
	private void previousRadio() {
		
			if(PlayerView.show == null)
				Toast.makeText(PlayerView.activity, PlayerView.activity.getResources().getString(R.string.channel_not_select), Toast.LENGTH_SHORT).show();
			else {
					
				List<Show> listShows = ListShowAdapter.data;

				if(listShows != null && listShows.size() > 0) {
					currentRadioIndex = Utils.getIndexShow(PlayerView.show, listShows);
					
					if(currentRadioIndex != -1) {
						if(currentRadioIndex > 0) {
							play(listShows.get(currentRadioIndex - 1));
							currentRadioIndex = currentRadioIndex - 1;
						}else{
							// play last radio
							play(listShows.get(listShows.size() - 1) );
							currentRadioIndex = listShows.size() - 1;
						}
					}
				}
				
			}
		}
	
	@Override
	public boolean onError(MediaPlayer mp, int arg1, int arg2) {
		// TODO Auto-generated method stub
		Log.e(TAG, "onError");
		// Changing button image to play button
		btnPlay.get().setImageResource(R.drawable.btn_play);
		tvTimerTotal.get().setText("");
		
		initializePlayer(PlayerView.show);
		
		Utils.showDialog(PlayerView.activity, PlayerView.activity.getResources().getString(R.string.alert), 
				PlayerView.activity.getResources().getString(R.string.error_open_channel));
		return false;
	}

	@Override
	public void onPrepared(MediaPlayer arg0) {
		// TODO Auto-generated method stub
		mp.start();
		
		tvTimerTotal.get().setText("");
		isPlaying = true;
		isPreparing = false;
		
		// Updating progress bar
		updateProgressBar();
	}
	
	@Override
	public void onCompletion(MediaPlayer arg0) {
		// TODO Auto-generated method stub
		mp.stop();
		isStop = false;
		isPlaying = false;
		isPreparing = false;
		btnPlay.get().setImageResource(R.drawable.btn_play);
		initializePlayer(PlayerView.show);
	}
	
	private void initializeUIElements() {
		
		tvHeaderTitle = new WeakReference<TextView>(PlayerView.tvHeaderTitle);
		tvHeaderDes = new WeakReference<TextView>(PlayerView.tvHeaderDes);
		btnHenGio = new WeakReference<ImageButton>(PlayerView.btnHome);
		btnStar = new WeakReference<ImageButton>(PlayerView.btnStar);
		
		imgView = new WeakReference<ImageView>(PlayerView.imgView);
		
		tvTimerCurrent = new WeakReference<TextView>(PlayerView.tvTimerCurrent);
		tvTimerTotal = new WeakReference<TextView>(PlayerView.tvTimerTotal);
		sbTimer = new WeakReference<SeekBar>(PlayerView.sbTimer);
		
		btnPlay = new WeakReference<ImageButton>(PlayerView.btnPlay);
		btnPlay.get().setImageResource(R.drawable.btn_play);
		btnPrevious = new WeakReference<ImageButton>(PlayerView.btnPrevious);
		btnNext = new WeakReference<ImageButton>(PlayerView.btnNext);
		btnBackward = new WeakReference<ImageButton>(PlayerView.btnBackward);
		btnForward = new WeakReference<ImageButton>(PlayerView.btnForward);
		
		tvHenGio = new WeakReference<TextView>(PlayerView.tvHenGio);
		
		if(clock != null) {
			clock.icon = btnHenGio.get();
			clock.clockFace = tvHenGio.get();
		}
		
		sbTimer.get().setOnSeekBarChangeListener(this); 
		btnHenGio.get().setOnClickListener(this);
		btnStar.get().setOnClickListener(this);
		btnNext.get().setOnClickListener(this);
		btnPrevious.get().setOnClickListener(this);
		btnPlay.get().setOnClickListener(this);
		btnBackward.get().setOnClickListener(this);
		btnForward.get().setOnClickListener(this);
		tvHenGio.get().setOnClickListener(this);
		btnHenGio.get().setOnClickListener(this);
		
		changeViewRadio(PlayerView.show);
	}
	
	private void changeViewRadio(Show show) {
		if(show == null) {
			tvHeaderTitle.get().setText(PlayerView.activity.getResources().getString(R.string.channel));
			tvHeaderDes.get().setText(PlayerView.activity.getResources().getString(R.string.channel_not_select));
			return;
		}
			//change iamge background
			imgView.get().setImageResource(com.htt.app.amnhac12h.utils.Utils.getImageRadom());

			// Play radio
			tvHeaderTitle.get().setText(show.getName());
			tvHeaderDes.get().setText(show.getDesc());
			
			DAOShow db = new DAOShow(getApplicationContext());
			db.open();
			if(db.checkShow(show))
				btnStar.get().setImageResource(R.drawable.icon_star_pressed);
			else
				btnStar.get().setImageResource(R.drawable.icon_star);
			db.close();
			
			// set Progress bar values
			sbTimer.get().setProgress(0);
			sbTimer.get().setMax(100);
			
			if(mp != null) {
				if(!isPlaying && !isPreparing) 
					btnPlay.get().setImageResource(R.drawable.btn_play);
				else 
					btnPlay.get().setImageResource(R.drawable.btn_pause);
			} else 
				btnPlay.get().setImageResource(R.drawable.btn_play);
	}
	
	private void initializePlayer(Show show) {
		if(show == null) {
			return;
		}
	    
	    releaseMediaPlayer();
	    
	    isStop = false;
	    isPlaying = false;
        isPreparing = false;
        mp = new MediaPlayer();
        
	    mp.setOnPreparedListener(this);
		mp.setOnErrorListener(this);
	    mp.setOnCompletionListener(this);
	    mp.setOnInfoListener(this);
	    mp.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
			
			@Override
			public void onBufferingUpdate(MediaPlayer arg0, int percent) {
				// TODO Auto-generated method stub
				Log.i(TAG, "Buffering: " + percent);
				sbTimer.get().setSecondaryProgress(percent);
			}
		});
	}
	
	public void releaseMediaPlayer() {
		mHandler.removeCallbacks(mUpdateTimeTask);
	    if (mp != null) {
	        mp.release();
	        mp = null;
	    }
	}
	
	/**
	 * Update timer on seekbar
	 * */
	private void updateProgressBar() {
        mHandler.postDelayed(mUpdateTimeTask, 100);    
    }
	
	/**
	 * Background Runnable thread
	 * */
	private Runnable mUpdateTimeTask = new Runnable() {
		   public void run() {
				   long totalDuration = mp.getDuration();
				   long currentDuration = mp.getCurrentPosition();
				  
				   // Displaying time completed playing
				    tvTimerTotal.get().setText("" + Utils.milliSecondsToTimer(totalDuration));
					tvTimerCurrent.get().setText("" + Utils.milliSecondsToTimer(currentDuration));
				   // Updating progress bar
				   int progress = (int)(Utils.getProgressPercentage(currentDuration, totalDuration));

				   sbTimer.get().setProgress(progress);
				   
				   // Running this thread after 100 milliseconds
			       mHandler.postDelayed(this, 100);
		   }
		};
		
	@Override
	public void onProgressChanged(SeekBar seekBar, int progress,
			boolean fromUser) {
		// TODO Auto-generated method stub
	}

	/**
	 * When user starts moving the progress handler
	 * */
	@Override
	public void onStartTrackingTouch(SeekBar seekBar) {
		// remove message Handler from updating progress bar
		mHandler.removeCallbacks(mUpdateTimeTask);
    }
	
	/**
	 * When user stops moving the progress hanlder
	 * */
	@Override
    public void onStopTrackingTouch(SeekBar seekBar) {
		mHandler.removeCallbacks(mUpdateTimeTask);
		if(isPlaying) {
			int totalDuration = (int) mp.getDuration();
			int currentPosition = Utils.progressToTimer(seekBar.getProgress(), totalDuration);
			
			// forward or backward to certain seconds
			mp.seekTo(currentPosition);
			
			// update timer progress again
			updateProgressBar();
		}
		
    }
	
	
	public void releaseThreadSlide() {
		if(threadSlide != null)
			threadSlide.interrupt();
	}
	
		public void playMedia() {
			if (!mp.isPlaying()) {
				mp.start();
				updateProgressBar();
			}
		}

		public void pauseMedia() {
			if (mp.isPlaying()) {
				mp.pause();
			}

		}

		public void stopMedia() {
			if (mp.isPlaying()) {
				mp.stop();
			}
		}

		
	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.e(TAG, "Player Service Stopped");
		
		isRun = false;
		isRunMedia = -1;
		releaseThreadSlide();
		// Remove progress bar update Hanlder callBacks
		mHandler.removeCallbacks(mUpdateTimeTask);
		
		if (mp != null) {
			if (mp.isPlaying()) {
				mp.stop();
			}
			mp.release();
			mp = null;
		}
		isPlaying = false;
		isPreparing = false;
		listended = null;
		
		if (phoneStateListener != null) {
			telephonyManager.listen(phoneStateListener,
					PhoneStateListener.LISTEN_NONE);
		}
		
	}
	// --------------------Push Notification
	// Set up the notification ID
	public static final int NOTIFICATION_ID = 211990;
	private NotificationManager mNotificationManager;

	// Create Notification
	@SuppressWarnings({ "deprecation", "deprecation" })
	private void initNotification() {
		String ns = Context.NOTIFICATION_SERVICE;
		mNotificationManager = (NotificationManager) getSystemService(ns);
		int icon = R.drawable.ic_launcher;
		CharSequence tickerText = getString(R.string.app_name);
		long when = System.currentTimeMillis();
		@SuppressWarnings("deprecation")
		Notification notification = new Notification(icon, tickerText, when);
		notification.flags = Notification.FLAG_ONGOING_EVENT;
		Context context = getApplicationContext();
		CharSequence name = PlayerView.show.getName();

		Intent notificationIntent = new Intent(this, AmNhac12h.class);
		notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
		PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
				notificationIntent, 0);
		notification.setLatestEventInfo(context, name, null, contentIntent);
		mNotificationManager.notify(NOTIFICATION_ID, notification);
	}

	@Override
	public boolean onInfo(MediaPlayer mp, int what, int extra) {
		// TODO Auto-generated method stub
		Log.e(TAG, "onInfo : " + what + ":" + extra);
		if(what == mp.MEDIA_INFO_BUFFERING_END) {
			Log.e(TAG, "MEDIA_INFO_BUFFERING_END");
		}
		else if(what == mp.MEDIA_INFO_BUFFERING_START) {
			Log.e(TAG, "MEDIA_INFO_BUFFERING_START");
		}
		else if(what == mp.MEDIA_INFO_UNKNOWN)
			Log.e(TAG, "MEDIA_INFO_DOWNLOAD_RATE_CHANGED");
		else if(what == mp.MEDIA_INFO_NOT_SEEKABLE)
			Log.e(TAG, "MEDIA_INFO_NOT_SEEKABLE");
		return false;
	}


}
